package cn.lu.controller;

import cn.lu.pojo.User;
import cn.lu.service.UserService;
import cn.lu.utils.Result;
import com.alibaba.fastjson.JSON;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

@Controller
@RequestMapping("/user")
@ResponseBody
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private JedisPool jedisPool;


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(@RequestBody User user){
        Jedis jedis = jedisPool.getResource();
        User nowUser = userService.login(user);
        String token = UUID.randomUUID()+"";
        String sUser = JSON.toJSONString(nowUser);
        jedis.set(token, sUser,"NX","EX",600);
        Map<String,Object> map = new HashMap<>();
        map.put("userId",nowUser.getId());
        map.put("userName",nowUser.getUserName());
        map.put("power",nowUser.getPower());
        map.put("token",token);
        return new Result().success(map);
    }



}